﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Pur;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Pur
{
    public partial class frmManagePurchaseQuoteOrder : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private PurQuoteOrderEntity mSelectedPurQuoteOrder;
        #endregion

        #region Constructor
        public frmManagePurchaseQuoteOrder()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCURRENCY, "CS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "STATUS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colUNIT, "UNIT", true);
        }

        private string GetCondition()
        {
            string condition = "";
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                MstSupplierEntity supplier = (MstSupplierEntity)txtSupplier.Tag;
                condition += " AND QO.SUPPLIER_ID = '" + supplier.SupplierId + "' ";
            }
            if (txtPurchaseOrder.Tag != null && txtPurchaseOrder.Tag.GetType() == typeof(PurPurchaseOrderEntity))
            {
                PurPurchaseOrderEntity entity = (PurPurchaseOrderEntity)txtPurchaseOrder.Tag;
                condition += " AND QO.PURCHASE_ORDER_ID = '" + entity.PurchaseOrderId + "' ";
            }
            if (string.IsNullOrEmpty(txtQuoteCode.Text) == false)
            {
                condition += " AND QO.QUOTE_ORDER_CODE LIKE '%" + txtQuoteCode.Text + "%'";
            }
            if (txtQuotePerson.Tag != null && txtQuotePerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtQuotePerson.Tag;
                condition += " AND QO.QUOTE_PERSON = '" + entity.PersonId + "' ";
            }
            else
            {
                if (!mCanQueryOtherPerson)
                {
                    condition += " AND QO.QUOTE_PERSON = '" + CommonParameter.CurrentUser.PersonId + "' ";
                }
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND QO.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (dtpQuoteDate.EditValue!=null)
            {
                condition += " AND QO.QUOTE_DATE >= CONVERT(DATETIME,'" + ((DateTime)dtpQuoteDate.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (dtpQuoteDateTo.EditValue != null)
            {
                condition += " AND QO.QUOTE_DATE <= CONVERT(DATETIME,'" +  ((DateTime)dtpQuoteDateTo.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (dtpDeliveryRequiredDate.EditValue != null)
            {
                condition += " AND QO.DELIVERY_REQUIRED_DATE >= CONVERT(DATETIME,'" +  ((DateTime)dtpDeliveryRequiredDate.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (dtpDeliveryRequiredDateTo.EditValue != null)
            {
                condition += " AND QO.DELIVERY_REQUIRED_DATE <= CONVERT(DATETIME,'" +  ((DateTime)dtpDeliveryRequiredDateTo.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "QO.QUOTE_ORDER_CODE DESC";
            string appId = "";
            if (txtPurApplication.Tag != null && txtPurApplication.Tag.GetType() == typeof(PurApplicationEntity))
            {
                appId = ((PurApplicationEntity)txtPurApplication.Tag).ApplicationId;
            }
            DataTable dt = PurQuoteOrderBs.GetByCondition(condition, appId, orderBy, rowCount);
            gvMain.DataSource = dt;
        }

        private void BindDetailGrid(string quoteOrderID)
        {
            if (string.IsNullOrEmpty(quoteOrderID))
            {
                gvDetail.DataSource = null;
                return;
            }
            DataSet ds = PurQuoteItemBs.GetDataSetByQuoteOrderID(quoteOrderID);
            if (ds == null || ds.Tables.Count == 0)
            {
                gvDetail.DataSource = null;
            }
            gvDetail.DataSource = ds.Tables[0];
        }

        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            btnPrint.Enabled = false;
            if (mSelectedPurQuoteOrder == null)
            {
                return;
            }
            if (mSelectedPurQuoteOrder.Status == NormalStatus.STATUS01.ToString()
                || mSelectedPurQuoteOrder.Status == NormalStatus.STATUS02.ToString()
                || mSelectedPurQuoteOrder.Status == NormalStatus.STATUS04.ToString())
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            else if (mSelectedPurQuoteOrder.Status == NormalStatus.STATUS03.ToString())
            {
                btnPrint.Enabled = true;
            }
            else if (mSelectedPurQuoteOrder.Status == NormalStatus.STATUS05.ToString())
            {
            }
            btnPrint.Enabled = true;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            mCanQueryOtherPerson = UICommon.UICommonFunctions.CheckPersonRight(RoleCategory.采购);
            BindComboBox();
            BindDataGrid();
            base.OnShown(e);
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedPurQuoteOrder = new PurQuoteOrderEntity(dr);
            BindDetailGrid(mSelectedPurQuoteOrder.QuoteOrderId);
            ResetButtonsEnable();
        }

        protected override void Click_View()
        {
            if (mSelectedPurQuoteOrder == null)
            {
                MessageBox.Show("请选择一个询价单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPurchaseQuoteOrder frm = new frmMaintainPurchaseQuoteOrder();
            frm.ThisActionType = ActionType.View;
            frm.SelectedPurQuoteOrder = mSelectedPurQuoteOrder;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedPurQuoteOrder == null)
            {
                MessageBox.Show("请选择一个询价单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中询价单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedPurQuoteOrder.Status = NormalStatus.STATUS05.ToString();
                mSelectedPurQuoteOrder.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedPurQuoteOrder.LastModifiedDate = DateTime.Now;
                bool successful = PurQuoteOrderBs.UpdatePurQuoteOrder(mSelectedPurQuoteOrder);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Submit()
        {
            if (mSelectedPurQuoteOrder == null)
            {
                MessageBox.Show("请选择一个询价单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否送审选中询价单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedPurQuoteOrder.Status = NormalStatus.STATUS02.ToString();
                mSelectedPurQuoteOrder.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedPurQuoteOrder.LastModifiedDate = DateTime.Now;
                bool successful = PurQuoteOrderBs.UpdatePurQuoteOrder(mSelectedPurQuoteOrder);
                if (successful)
                {
                    MessageBox.Show("送审成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedPurQuoteOrder == null)
            {
                MessageBox.Show("请选择一个询价单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPurchaseQuoteOrder frm = new frmMaintainPurchaseQuoteOrder();
            frm.ThisActionType = ActionType.Approve;
            frm.SelectedPurQuoteOrder = mSelectedPurQuoteOrder;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedPurQuoteOrder == null)
            {
                MessageBox.Show("请选择一个询价单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPurchaseQuoteOrder frm = new frmMaintainPurchaseQuoteOrder();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedPurQuoteOrder = mSelectedPurQuoteOrder;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainPurchaseQuoteOrder frm = new frmMaintainPurchaseQuoteOrder();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Print()
        {
            if (mSelectedPurQuoteOrder == null)
            {
                MessageBox.Show("请选择一个询价单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            List<PurQuoteOrderEntity> list = new List<PurQuoteOrderEntity>();
            //list.Add(mSelectedPurQuoteOrder);
            //frmFindPrinter frmPrinter = new frmFindPrinter();
            //if (frmPrinter.ShowDialog() == DialogResult.OK)
            //{
            //    frmPrintPurchaseQuoteOrder frm = new frmPrintPurchaseQuoteOrder(list);
            //    frm.SelectedPrinterName = frmPrinter.SelectedPrinterName;
            //    frm.DocumentName = mSelectedPurQuoteOrder.QuoteOrderCode;
            //    frm.Print();
            //}
        }

        private void txtSupplier_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainSupplierQuery frm = new frmMaintainSupplierQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedSupplier != null)
            {
                txtSupplier.Text = frm.SelectedSupplier.SupplierName;
                txtSupplier.Tag = frm.SelectedSupplier;
            }
            else
            {
                txtSupplier.Text = "";
                txtSupplier.Tag = null;
            }
        }

        private void txtPurchaseOrder_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainPurchaseOrderQuery frm = new frmMaintainPurchaseOrderQuery(false);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPurchaseOrder != null)
            {
                txtPurchaseOrder.Tag = frm.SelectedPurchaseOrder;
                txtPurchaseOrder.Text = frm.SelectedPurchaseOrder.PurchaseOrderCode;
            }
            else
            {
                txtPurchaseOrder.Text = "";
                txtPurchaseOrder.Tag = null;
            }
        }

        private void txtPurApplication_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainPurchaseApplicationQuery frm = new frmMaintainPurchaseApplicationQuery(false);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPurApplication != null)
            {
                txtPurApplication.Text = frm.SelectedPurApplication.ApplicationCode;
                txtPurApplication.Tag = frm.SelectedPurApplication;
            }
            else
            {
                txtPurApplication.Text = "";
                txtPurApplication.Tag = null;
            }
        }

        private void txtQuotePerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtQuotePerson.Text = frm.SelectedPerson.NameCn;
                txtQuotePerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtQuotePerson.Text = "";
                txtQuotePerson.Tag = null;
            }
        }

        #endregion
    }
}
